package com.cskaoyan.catmall.controller.wx;

import com.cskaoyan.catmall.model.bo.SaveAddressBo;
import com.cskaoyan.catmall.model.pojo.MarketUser;
import com.cskaoyan.catmall.model.vo.AddressVo;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.GetAddressListVo;
import com.cskaoyan.catmall.service.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 创建日期: 2022/01/10 20:23
 *
 * @author WangZhe
 * 备注:
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {

    @Autowired
    WxAddressService wxAddressService;


    @GetMapping("list")
    public BaseRespVo getList() {

//        Integer userId = 99;
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        Integer userId = user.getId();

        GetAddressListVo getAddressListVo = wxAddressService.getList(userId);
        return BaseRespVo.ok(getAddressListVo);
    }

    @GetMapping("detail")
    public BaseRespVo getDetail(@RequestParam("id") Integer addressId) {

        AddressVo vo = wxAddressService.getDetail(addressId);
        return BaseRespVo.ok(vo);
    }

    @PostMapping("save")
    public BaseRespVo saveAddress(@RequestBody SaveAddressBo saveAddressBo) {

//        Integer userId = 99;
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        Integer userId = user.getId();

        Integer id = wxAddressService.saveAddress(userId, saveAddressBo);
        return BaseRespVo.ok(id);
    }

    @PostMapping("delete")
    public BaseRespVo deleteAddress(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        wxAddressService.deleteAddress(id);
        return BaseRespVo.ok(id);
    }

}
